﻿package _A_TJTable;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 * 2011-6-2 12:26:47
 * 
 * @author Administrator 直接通过传递给JTable的Vector动态修改JTable
 */
public class JTable_Vector {
    JFrame frame;
    Container contentPane;
    JScrollPane jsp;
    JTable table;
    Vector<String> vName;
    Vector<Vector<Object>> vData;
    JButton addButton, deleteButton;

    public JTable_Vector() {
        frame = new JFrame("表格组件测试");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = frame.getContentPane();
        contentPane.setLayout(new BorderLayout());
        String name[] = { "姓名", "语文", "数学" };
        Object data[][] = { { "王鹏", 91, 100 }, { "李兵", 99, 89 }, { "肖龙", 100, 100 } };
        vName = new Vector<String>();
        for (int i = 0; i < name.length; i++) {
            vName.add(name[i]);
        }
        vData = new Vector<Vector<Object>>();
        for (int i = 0; i < data.length; i++) {
            Vector<Object> tmp = new Vector<Object>();
            for (int j = 0; j < data[i].length; j++) {
                tmp.add(data[i][j]);
            }
            vData.add(tmp);
        }
        table = new JTable(vData, vName);
        table.setPreferredScrollableViewportSize(new Dimension(400, 100));
        jsp = new JScrollPane(table);
        contentPane.add(jsp, BorderLayout.CENTER);
        addButton = new JButton("添加");
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Vector<Object> v = new Vector<Object>();
                final Object def[] = { "", "", "" };
                for (int i = 0; i < def.length; i++) {
                    v.add(def[i]);
                }
                vData.add(v);
                table.updateUI();
                // table.repaint();
            }
        });
        deleteButton = new JButton("删除");
        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                vData.remove(vData.size() - 1);
                table.updateUI();
            }
        });
        Box box = Box.createHorizontalBox();
        box.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        box.add(Box.createHorizontalGlue());
        box.add(addButton);
        box.add(Box.createHorizontalStrut(10));
        box.add(deleteButton);
        box.add(Box.createHorizontalGlue());
        box.setMaximumSize(box.getPreferredSize());
        contentPane.add(box, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        new JTable_Vector();
    }
}
